package com.compass.doctor.ui.patient

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.compass.doctor.R
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.databinding.ActivityInspectionReportListBinding
import com.compass.doctor.ui.patient.fragment.InspectionReportFragment
import com.compass.framework.adapter.ViewPage2FragmentAdapter
import com.compass.framework.constant.KEY_INDEX
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.ext.inflateLayout
import com.compass.framework.ext.toBeanOrNull
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.Dictionaries

/**
 * Description： 检查检验--记录（患者）
 * Date:2024/3/26 9:11
 */
class InspectionReportListActivity : BaseVBActivity<ActivityInspectionReportListBinding>() {


    companion object {
        fun start(context: Context, patientId: String,index:Int) {
            val intent = Intent(context, InspectionReportListActivity::class.java)
            intent.putExtra(KEY_PATIENT_ID, patientId)
            intent.putExtra(KEY_INDEX, index)
            context.startActivity(intent)
        }
    }

    private lateinit var viewPageAdapter: ViewPage2FragmentAdapter
    private var fragments: ArrayList<Fragment> = ArrayList()
    private lateinit var patientId: String
    private var index=0
    override fun initView(savedInstanceState: Bundle?) {
        patientId = intent.getStringExtra(KEY_PATIENT_ID)!!
        index = intent.getIntExtra(KEY_INDEX,0)
        val list =
            DictionariesManager.getInspectionReportType()?.toBeanOrNull<MutableList<Dictionaries>>()
        list?.let {
            mBinding.dslTab.apply {
                tabIndicator.indicatorWidth = -2
                tabIndicator.indicatorEnableFlow = true
                for (item in list) {
                    val inspectionReportFragment = InspectionReportFragment()
                    val bundleRx = Bundle()
                    bundleRx.putString(KEY_ORDER_TYPE, item.value)
                    bundleRx.putString(KEY_PATIENT_ID, patientId)
                    inspectionReportFragment.arguments = bundleRx
                    fragments.add(inspectionReportFragment)
                    inflateLayout(R.layout.layout_text_view).apply {
                        findViewById<TextView>(R.id.text_view)?.text=item.label
                        addView(this@apply)
                    }
                }

            onTabBadgeConfig={_, tabBadge, index ->
                tabBadge.badgeText=  list[index].label
                null
            }
            }
            initDslTab()
        }


    }


    private fun initDslTab() {
        mBinding.dslTab.configTabLayoutConfig {
            onSelectIndexChange = { _, selectIndexList, _, _ ->
                val toIndex = selectIndexList.first()
                mBinding.viewpager2.setCurrentItem(toIndex, false)

            }
        }
        viewPageAdapter = ViewPage2FragmentAdapter(supportFragmentManager, lifecycle, fragments)
        mBinding.viewpager2.adapter = viewPageAdapter
        mBinding.viewpager2.isUserInputEnabled = false//禁止viewPage滑动
        mBinding.dslTab.setCurrentItem(index)
//        mBinding.viewpager2.setCurrentItem(0, false)
    }


}